﻿using System.Windows;
using NUnit.Framework;
using System.Linq;

namespace Utility3D
{
    [TestFixture]
    public class SingleSurfacePointRefTest
    {
        [Test]
        public void WhenDrawingSingleSurfacePointRefItAppearsOnOtherSurfaces()
        {
            var worldModel = DefaultWorldModelGenerator.GetModel();

            var drawingSurface = worldModel.DrawingSurfaceChains.First(ds => ds.Name == "Front View");

            var p1 = worldModel.Collections.AddPoint<SingleSurfacePointRef>(drawingSurface, new Point(10, 10));
            var p2 = worldModel.Collections.AddPoint<SingleSurfacePointRef>(drawingSurface, new Point(90, 90));
            worldModel.Collections.AddStroke<LineStroke>(p1, p2);

            drawingSurface = worldModel.DrawingSurfaceChains.First(ds => ds.Name == "Top View").Glide(50);
            var points = worldModel.GetIntersectionPointsOnSurface(drawingSurface);

            Assert.IsTrue(points.Any());
        }
    }

    public class SingleSurfacePointRef : IPointRef
    {
        private Point3D _point;
        public Point3D Point
        {
            get { return _point; }
        }

        public int Id { get; set; }

        public Point? GetOnSurface(DrawingSurface drawingSurface)
        {
            var point = drawingSurface.Plane.ToPlaneCoordinates(_point);
            if (point.Z != 0) return null;
            return new Point(point.X, point.Y);
        }

        public Point3D? GetPoint3DOnSurface(DrawingSurface drawingSurface)
        {
            return _point;
        }

        public void SetOnSurface(DrawingSurface drawingSurface, Point point)
        {
            var xVector = drawingSurface.Plane.VectorX*(point.X/drawingSurface.Width);
            var yVector = drawingSurface.Plane.VectorY*(point.Y/drawingSurface.Height);
            _point = drawingSurface.Plane.BasePoint + xVector + yVector;
        }
    }
}